package com.lixin.yun_notebook.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import bean.Note;

import com.lixin.yun_notebook.R;

public class NoteAdapter extends BaseAdapter {
	private LayoutInflater inflater=null;
	private List<Note> rs=null;
	public boolean flage = false; 
	public NoteAdapter(Context c,List<Note> rs) {
		inflater=LayoutInflater.from(c);
		this.rs=rs;
	}
	@Override
	public int getCount() {
		return rs.size();
	}
	@Override
	public Object getItem(int position) {
		return rs.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null; 
		if(convertView==null){
			convertView=inflater.inflate(R.layout.item_note, parent,false);
			
			holder = new ViewHolder();  
			holder.checkboxOperateData = (CheckBox) convertView.findViewById(R.id.checkbox_operate_data);  
            holder.text = (TextView) convertView.findViewById(R.id.text);  
            holder.date = (TextView) convertView.findViewById(R.id.date);  
            convertView.setTag(holder);  
		}else{
			holder = (ViewHolder) convertView.getTag();  
		}
		convertView.findViewById(R.id.head);
		final Note n=rs.get(position);
		
		if (n != null) {
			holder.text.setText(n.getContent());
			holder.date.setText(n.getCreatedAt().substring(0, 10));
			
			// isSelectedcheckboxʾ״  
            if (flage) {  
                holder.checkboxOperateData.setVisibility(View.VISIBLE);  
            } else {  
                holder.checkboxOperateData.setVisibility(View.GONE);  
            }  
            
            holder.checkboxOperateData.setChecked(n.getIsCheck());
            
            holder.checkboxOperateData.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    if (n.getIsCheck()) {  
                        n.setIsCheck(false);  
                    } else {  
                        n.setIsCheck(true);  
                    }  
                }  
            });  
		}
		return convertView;
	}
	
	class ViewHolder {  
		  
        public CheckBox checkboxOperateData;  
  
        public TextView text;  
  
        public TextView date;  
    }

}
